package com.the4man.framework.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

/**
 * Created by lenovo on 2017/12/12.
 */
public class JsonHelper {
    public JsonHelper() {
    }

    public static String getJSONByList(List<?> list) {
        String retStr = "";
        StringBuffer buffer = new StringBuffer();
        if(list != null && list.size() > 0) {
            buffer.append("[");

            for(int i = 0; i < list.size(); ++i) {
                buffer.append(list.get(i) != null?list.get(i).toString():"").append(",");
            }

            retStr = buffer.substring(0, buffer.length() - 1).concat("]");
        }

        return retStr;
    }

    public static String getJSONByMap(Map<String, Object> map) {
        Iterator<?> iter = map.entrySet().iterator();
        StringBuffer buffer = new StringBuffer();
        buffer.append("{");

        while(iter.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry)iter.next();
            buffer.append("\"").append(entry.getKey() != null?(String)entry.getKey():"").append("\":").append("\"").append(entry.getValue() != null?entry.getValue():"").append("\",");
        }

        return buffer.substring(0, buffer.length() - 1).concat("}");
    }

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap();
        Map<String, Object> map2 = new HashMap();
        map.put("DATA_DATE", "2012-10-01");
        map.put("CITY_ID", "999");
        map.put("BRAND_ID", "-1");
        map.put("RANK_ID", "0");
        map2.put("DATA_DATE", "2012-10-01");
        map2.put("CITY_ID", "123");
        map2.put("BRAND_ID", "-1");
        map2.put("RANK_ID", "1");
        List<Map<String, Object>> list = new ArrayList();
        list.add(map);
        list.add(map2);
        List<String> jsonMapList = new ArrayList();
        Iterator var6 = list.iterator();

        while(var6.hasNext()) {
            Map<String, Object> objMap = (Map)var6.next();
            String jsonMap = getJSONByMap(objMap);
            jsonMapList.add(jsonMap);
        }

        String tasUserAagJSON = getJSONByList(jsonMapList);
        StringBuffer newJsonBuf = new StringBuffer();
        newJsonBuf.append("{\"list\":").append(tasUserAagJSON).append("}");
        tasUserAagJSON = newJsonBuf.toString();
        System.out.println(tasUserAagJSON);
    }

    public static String getJSONByBeanList(List<?> list) {
        StringBuffer buffer = new StringBuffer();

        try {
            Class<?> clazz = list.get(0).getClass();
            Field[] fields = clazz.getDeclaredFields();
            buffer.append("[");

            for(int i = 0; i < list.size(); ++i) {
                buffer.append("{");

                for(int j = 0; j < fields.length; ++j) {
                    String fieldName = fields[j].getName();
                    String firstLetter = fieldName.substring(0, 1).toUpperCase();
                    String getMethodName = "get" + firstLetter + fieldName.substring(1);
                    Method getMethod = clazz.getMethod(getMethodName, new Class[0]);
                    Object value = getMethod.invoke(list.get(i), new Object[0]);
                    buffer.append("\"" + fieldName + "\":\"" + value + "\",");
                }

                buffer = new StringBuffer(buffer.substring(0, buffer.length() - 1));
                buffer.append("},");
            }
        } catch (SecurityException var11) {
            var11.printStackTrace();
        } catch (NoSuchMethodException var12) {
            var12.printStackTrace();
        } catch (IllegalArgumentException var13) {
            var13.printStackTrace();
        } catch (IllegalAccessException var14) {
            var14.printStackTrace();
        } catch (InvocationTargetException var15) {
            var15.printStackTrace();
        }

        return buffer.substring(0, buffer.length() - 1).concat("]");
    }

    public static String getJSONByBean(Object object) {
        StringBuffer buffer = new StringBuffer();

        try {
            Class<?> clazz = object.getClass();
            Field[] fields = clazz.getDeclaredFields();
            buffer.append("[{");

            for(int j = 0; j < fields.length; ++j) {
                String fieldName = fields[j].getName();
                String firstLetter = fieldName.substring(0, 1).toUpperCase();
                String getMethodName = "get" + firstLetter + fieldName.substring(1);
                Method getMethod = clazz.getMethod(getMethodName, new Class[0]);
                Object value = getMethod.invoke(object, new Object[0]);
                buffer.append("\"" + fieldName + "\":\"" + value + "\",");
            }
        } catch (SecurityException var10) {
            var10.printStackTrace();
        } catch (NoSuchMethodException var11) {
            var11.printStackTrace();
        } catch (IllegalArgumentException var12) {
            var12.printStackTrace();
        } catch (IllegalAccessException var13) {
            var13.printStackTrace();
        } catch (InvocationTargetException var14) {
            var14.printStackTrace();
        }

        return buffer.substring(0, buffer.length() - 1).concat("}]");
    }

    public static String getXMLByJSON(Object object) {
        return null;
    }

    public static Map parseJSON2Map(String jsonStr) {
        Map map = new HashMap();
        JSONObject json = JSONObject.fromObject(jsonStr);
        Iterator var4 = json.keySet().iterator();

        while(true) {
            while(var4.hasNext()) {
                Object k = var4.next();
                Object v = json.get(k);
                if(v instanceof JSONArray) {
                    List list = new ArrayList();
                    Iterator it = ((JSONArray)v).iterator();

                    while(it.hasNext()) {
                        JSONObject json2 = (JSONObject)it.next();
                        list.add(parseJSON2Map(json2.toString()));
                    }

                    map.put(k.toString(), list);
                } else {
                    map.put(k.toString(), v);
                }
            }

            return map;
        }
    }

    public static Object parseJSONOBject(String jsonStr, Class clazz) {
        JSONObject jsonObject = JSONObject.fromObject(jsonStr);
        return JSONObject.toBean(jsonObject, clazz);
    }
}
